Applying Functions
df.get(column_name).apply(function_name)
Applies a function of one parameter to every entry in the column.
- Input:
- function_name: a python function
- The function to apply to every entry in the column. This function should take a single parameter and return a value.
- Returns:
- A Series of the same size containing the results of the function application.
- Return Type:
- Series:
- The returned Series will have the same index as the input Series and will contain the transformed values based on the applied function.
pets
Index | Species | Color | Weight | Age |
---|---|---|---|---|
0 | dog | black | 40 | 5 |
1 | cat | golden | 15 | 8 |
2 | cat | black | 20 | 9 |
3 | dog | white | 80 | 2 |
4 | dog | black | 25 | 0.5 |
5 | hamster | black | 1 | 3 |
6 | hamster | golden | 0.25 | 0.2 |
pets.get('Species').apply(is_dog)
- 0"True"
- 1"False"
- 2"False"
- 3"True"
- 4"True"
- 5"False"
- 6"False"
pets.get('Weight').apply(np.sqrt)
- 0"6.324555"
- 1"3.872983"
- 2"4.472136"
- 3"8.944272"
- 4"5.000000"
- 5"1.000000"
- 6"0.500000"
(Refer back to Writing Functions for categorize_animal.)
pets.get('ID').apply(categorize_animal)
- 0"Adult Normal"
- 1"Kitten Underweight"
- 2"Adult Overweight"
- 3"Adult Overweight"
- 4"Puppy Normal"
- 5"Senior Overweight"
- 6"Young Normal"
- 7"Kitten Normal"